package com.gaff.emp.core.modules.perm.controller.user;

import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.R;
import com.gaff.emp.core.modules.auth.LoginUserInfo;
import com.gaff.emp.core.modules.auth.annotation.Auth;
import com.gaff.emp.core.modules.auth.annotation.LoginUser;
import com.gaff.emp.core.modules.perm.controller.user.form.*;
import com.gaff.emp.core.modules.perm.entity.SysUser;
import com.gaff.emp.core.modules.perm.service.SysUserService;
import com.gaff.emp.core.syslog.annotation.SysOperaLog;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    @ApiOperation("分页获取用户列表")
    @SysOperaLog(value = "分页获取用户列表", isPersistence = false)
    @Auth(perm = "user")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok(sysUserService.queryUsersByPage(params));
    }


    @GetMapping("/info/{id}")
    @ApiOperation("获取用户信息")
    @SysOperaLog(value = "获取用户信息", isPersistence = false)
    @Auth(perm = "user")
    public R info(@PathVariable("id") Integer id) {
        SysUser user = sysUserService.queryById(id);
        if (user == null) {
            throw new EmpException("用户不存在");
        }
        user.setPassword("");
        return R.ok(user);
    }

    @PostMapping("/create")
    @ApiOperation("新增用户")
    @SysOperaLog(value = "新增用户")
    @Auth(perm = "user")
    public R create(@RequestBody @Valid CreateForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysUserService.createUser(form);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("更新用户")
    @SysOperaLog(value = "更新用户")
    @Auth(perm = "user")
    public R update(@RequestBody @Valid UpdateForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysUserService.updateUser(form);
        return R.ok();
    }

    @PostMapping("/status")
    @ApiOperation("禁用或启用用户")
    @SysOperaLog(value = "禁用或启用用户")
    @Auth(perm = "user")
    public R status(@RequestBody @Valid StatusForm form, @LoginUser LoginUserInfo userInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        if (userInfo.getUser().getId() == form.getId()) {
            throw new EmpException("不能修改自身的状态");
        }
        sysUserService.enableOrDisableUser(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    @SysOperaLog(value = "删除用户")
    @Auth(perm = "user")
    public R delete(@RequestBody @Valid DeleteForm form, @LoginUser LoginUserInfo userInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        if (userInfo.getUser().getId() == form.getId()) {
            throw new EmpException("不能删除自身");
        }
        sysUserService.deleteById(form.getId());
        return R.ok();
    }

    @PostMapping("/password")
    @ApiOperation("修改密码")
    @SysOperaLog(value = "修改密码")
    @Auth
    public R password(@RequestBody @Valid PasswordForm form, @LoginUser LoginUserInfo userInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysUserService.updatePassword(userInfo, form);
        return R.ok();
    }

}
